package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill168Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-每次攻击额外减少当前生命的{0}%，增加气势{1}%
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //基础攻击miss，不触发技能
        if (baseAttReHpResult == null) {
            return false;
        }
        int attackTimes = baseAttSkill.getSkillEffect().getP2();
        //每次攻击额外减少当前生命的{0}%，增加气势{1}%
        for (int i = 0; i < attackTimes; i++) {
            long decHp = BigDecimal.valueOf(action.getHp()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            if (decHp > action.getHp() && action.getHp() > 1) {
                decHp = action.getHp() - 1;
            }

            CricketProto.CricketBattleFrame.Builder skill168FrameBuilder = cricketGame.newBattleFrameBuilder();
            skill168FrameBuilder.setActionUserId(action.getUserId());
            skill168FrameBuilder.setTargetUserId(action.getUserId());
            skill168FrameBuilder.setSkillId(skillData.getSkillId());
            if (decHp > 0) {
                AddHpResult actionAddHpResult = action.addHp(-decHp);
                skill168FrameBuilder.setAttack(actionAddHpResult.getValue());
                skill168FrameBuilder.setIsKill(actionAddHpResult.isKill());
                skill168FrameBuilder.setHarmEffectSkillId(actionAddHpResult.getReduceHpEffectSkillId());
            }
            cricketGame.fillRemainHp(skill168FrameBuilder);
            long addPower = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP2())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            action.addPower(addPower);
            skill168FrameBuilder.setAddPower(addPower);
            attackRoundBuilder.addAttack(skill168FrameBuilder);
        }
        return false;
    }
}
